

package yf.mall.service.impl;

import com.yf.response.RRException;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.vo.up.SecCodeUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;

import java.io.InputStream;

import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yf.mall.vo.down.SecCodeDown;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;

import yf.mall.vo.down.SecCodeDown;

import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

import yf.mall.dao.SecCodeDao;
import yf.mall.entity.SecCodeEntity;
import yf.mall.vo.SecCodeVo;
import yf.mall.service.SecCodeService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Fri Jul 28 15:20:42 CST 2023
 */
@Service
public class SecCodeServiceImpl extends BaseServiceImpl<SecCodeEntity> implements SecCodeService {

    private SecCodeDao dao;


    @Autowired
    public void setDao(SecCodeDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }


    @Override
    public List<SecCodeVo> findVo(SecCodeVo vo) {
        return dao.findVo(vo);
    }

    @Override
    public SecCodeVo getVo(Long id) {
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(SecCodeVo vo) {
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(SecCodeVo vo) {
        return dao.update(vo.getEntity());
    }


    @Override
    public SecCodeEntity findByCode(String code) {
        SecCodeEntity secCodeEntity = dao.findByCode(code);
        if (secCodeEntity == null) {
            throw new RRException("防伪码不存在");
        } else {
            if (secCodeEntity.getNum() == null) {
                secCodeEntity.setNum(1);
            } else {
                //次数累计一次
                secCodeEntity.setNum(secCodeEntity.getNum() + 1);
            }
        }
        dao.update(secCodeEntity);
        return secCodeEntity;
    }

    @Override
    public void downLoad(HttpServletResponse response, List<SecCodeVo> list) {
        try {
            List<SecCodeDown> datas = list.stream().map(item -> new SecCodeDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), SecCodeDown.class).sheet("模板").doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, SecCodeUp.class, new UploadDataListener()).sheet().doRead();
    }

    class UploadDataListener extends AnalysisEventListener<SecCodeUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(SecCodeUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }


    /**内部方法**/


    /**外部方法实现**/


}
